﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace TelnetSniffer
{
    class Sniff
    {
        static int ip1 = 192;
        static int ip2 = 168;
        static int ip3 = 1;
        static int ip4 = 148;
        static String ip = "192.168.1.148";
        static void Main(string[] args)
        {
            while (ip != "192.168.1.254")
            {
                sniff(ip);
                ip = inc(ip);
            }
        }
        static void sniff(string ip)
        {
            try
            {
                 TelnetConnection tl = new TelnetConnection(ip, 23);
                 Sniff.capture(tl.Read());
                 Console.WriteLine("CONNECTED AT: " + ip);
            }
            catch
            {
                Sniff.blacklist(ip);
                Console.WriteLine("NO CONNECTION: " + ip);
            }
        }
        private static string inc(string ipa)
        {
            if (ip4 == 254)
            {
                ip4 = 1;
                ip3++;
            }
            ip4++;
            ipa = ip1 + "." + ip2 + "." + ip3 + "." + ip4;
            return ipa;
        }
        private static void blacklist(string ip)
        {
            TextWriter black = File.AppendText("Blacklist.txt");
            black.WriteLine(ip);
            black.Close();
        }
        private static void capture(string outp)
        {
            TextWriter output = File.AppendText("Output.txt");
            output.WriteLine("--------------------------------------------------------");
            output.WriteLine("OUTPUT - \r\n" + outp + "\r\n");
            output.WriteLine(DateTime.Now);
            output.WriteLine("IP: " + ip);
            output.Close();
        }       
    }
}
